package com.itheima.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findPage")
    public List<User> findPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer pageSize) {
        return userService.findPage(page, pageSize);
    }

    @GetMapping(value = "/downLoadXlsByJxl", name = "使用jxl下载")
    public void downLoadXlsByJxl(HttpServletResponse response) {
        //userService.downLoadXlsByJxl(response);
    }

    @PostMapping(value = "/uploadExcel", name = "上传用户信息")
    public void uploadExcel(MultipartFile file) throws Exception {
//        userService.uploadExcel(file);
        userService.uploadExcelWithEasyPOI(file);
    }

/*    @GetMapping(value = "/downLoadXlsxByPoi", name = "使用POI导出Excel")
    public void downLoadXlsxByPoi(HttpServletResponse response) {
//        userService.downLoadXlsxByPoi(response);
//        userService.downLoadXlsxByPoiWithCellStyle(response);
        userService.downLoadXlsxByPoiWithTemplate(response);
    }*/

    @GetMapping(value = "/downLoadXlsxByPoi", name = "使用POI导出Excel")
    public void downLoadXlsxByPoi(HttpServletRequest request, HttpServletResponse response) throws Exception {
        userService.downLoadXlsxByPoiWithTemplate(request, response);
    }

    @GetMapping(value = "/download", name = "导出用户详细信息")
    public void downLoadUserInfoWithTemplate(Long id, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        userService.downLoadUserInfoWithTemplate(id, request, response);
//        userService.downLoadUserInfoWithTemplate2(id, request, response);
//        userService.downLoadUserInfoWithEastPOI(id, response, response);
        userService.downLoadUserInfoPDF(id, response);
    }

    @GetMapping(value = "/downLoadMillion", name = "用户百万数据的导出")
    public void downLoadMillion(HttpServletResponse response) throws Exception {
//        userService.downLoadUserInfoWithTemplate(id, request, response);
        userService.downLoadMillion(response);
    }


    @GetMapping(value = "/downLoadCSV", name = "导出用户数据到CSV文件中")
    public void downLoadCSV(HttpServletResponse response) throws Exception {
//        userService.downLoadCSV(response);
        userService.downLoadCSVWithEasyPOI(response);
    }

    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Long id) {
        return userService.findById(id);
    }

    @GetMapping(value = "/downloadContract", name = "导出用户合同")
    public void downloadContract(Long id, HttpServletResponse response) throws Exception {
//        userService.downloadContract(id, response);
        userService.downloadContractWithEasyPOI(id, response);
    }

    @GetMapping(value = "/downLoadWithEasyPOI", name = "使用EasyPOI下载Excel")
    public void downLoadWithEasyPOI(Long id, HttpServletResponse response) throws Exception {
        userService.downLoadWithEasyPOI(id, response);
    }

    @GetMapping(value = "/downLoadPDF", name = "导出PDF")
    public void downLoadPDF(HttpServletResponse response) throws Exception {
//        userService.downLoadPDFByDB(response);
        userService.downLoadPDF(response);
    }


}
